/*
 *
 * $Id: file.java 000 2006-09-01 00:00:00Z author $
 */
package logmatcher.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 *  
DOCUMENT ME!
 *
 *  @author $Author: author $
 *  @version $Rev: 000 $
 *
 *  @todo DOCUMENT ME!
 */
public class TimestampGenerator {
    //~ Static fields/initializers -------------------------------------------------------------------------------------

	/** Class logger. */
    private static final Log LOGGER = LogFactory.getLog(TimestampGenerator.class);
	
    /** TODO: DOCUMENT ME! */
    private static String pattern = "yyyy-MM-dd-HH-mm-ss";

    /** TODO: DOCUMENT ME! */
    private static SimpleDateFormat sdf = new SimpleDateFormat(pattern);

    //~ Methods --------------------------------------------------------------------------------------------------------

    /**
     *  
    DOCUMENT ME!
     *
     *  @return TODO: DOCUMENT ME!
     *
     *  @todo DOCUMENT ME!
     */
    public static String getPattern() {
        return pattern;
    }


    /**
     *  
    DOCUMENT ME!
     *
     *  @param pattern TODO: DOCUMENT ME!
     *
     *  @todo DOCUMENT ME!
     */
    public static void setPattern(String pattern) {
        try {
            sdf.applyPattern(pattern);
            TimestampGenerator.pattern = pattern;
        } catch (IllegalArgumentException e) {
            LOGGER.error("Illegal pattern has been supplied: "+pattern, e);
        }
    }


    /**
     *  
    DOCUMENT ME!
     *
     *  @return TODO: DOCUMENT ME!
     *
     *  @todo DOCUMENT ME!
     */
    public static String getTimestamp() {
        return sdf.format(new Date());
    }


    /**
     *  Return timestamp formatted according to specified pattern. If the pattern is invalid old pattern is
     *  used.
     *
     *  @param tmpPattern
     *
     *  @return
     */
    public static String getTimestamp(String tmpPattern) {
        String timestamp = sdf.format(new Date());

        try {
            sdf.applyPattern(tmpPattern);
            timestamp = sdf.format(new Date());
        } catch (IllegalArgumentException e) {
            LOGGER.warn("Illegal pattern has been supplied: "+pattern+", using previous one", e);
        }

        return timestamp;
    }
}
